<?php

namespace App\Backend\Http\Controllers;

use App\Cat;
use App\Map;
use Illuminate\Http\Request;
use Illuminate\Http\Response;
use Illuminate\Routing\Controller;

class MapController extends Controller
{
    /**
     * Display a listing of the resource.
     * @return Response
     */
    public function index($type,$cat_id1)
    {
        $where[] = ['id','>',0];
        !empty($cat_id1)?$where['cat_id1'] = $cat_id1:'';
        !empty(request('title'))?$where[] = ['title','like','%'.request('title').'%']:'';
        $data['cats'] = Cat::where('type',$type)->get();
        $data['maps'] = Map::where($where)->orderBy('id','desc')->paginate(20);
        $data['cat_id1'] = $cat_id1;
        return view('backend::map.map',$data);
    }

    /**
     * Show the form for creating a new resource.
     * @return Response
     */
    public function create($id)
    {
        $data['map'] = Map::find($id);
        $data['cats'] = Cat::where('type',6)->get() ;
        return view('backend::map.mapedit',$data);
    }

    /**
     * Store a newly created resource in storage.
     * @param  Request $request
     * @return Response
     */
    public function store(Request $request)
    {
        $data = $request->except('_token');
        if(isset($data['id'])){
            Map::where('id',$data['id'])->update($data);
            return redirect('backend/prompt')->with(['message'=>'修改成功！','status'=>true]);
        }else{
            $data['title'] = explode('.',$data['title'])[0];
            $data['status'] = 1;
            $data['id'] = Map::create($data);
            return $data;
        }
    }

    /**
     * Show the specified resource.
     * @return Response
     */
    public function show()
    {
        return view('backend::show');
    }

    /**
     * Show the form for editing the specified resource.
     * @return Response
     */
    public function edit()
    {
        return view('backend::edit');
    }

    /**
     * Update the specified resource in storage.
     * @param  Request $request
     * @return Response
     */
    public function update(Request $request)
    {
    }

    /**
     * Remove the specified resource from storage.
     * @return Response
     */
    public function destroy($id)
    {
        Map::where('id',$id)->delete();
        return redirect('/backend/map/6/0');
    }
}
